﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using Buccaneer;
using System.Diagnostics;
using Buccontrols;
using PluginInterface;

namespace Buccailor
{
    public partial class FileBrowser
    {
        #region Constructor


        #endregion

        #region Public Attributes

        //public static ListviewButton[] tempLvButtons;
        public static string currentPath;
        public static float zoom;
        public static int Padding, ReturnWidth;
        public static Size IconSize;

        #endregion

        #region Private Functions

        private Bitmap GetFileIcon(string extension)
        {
            string text = ".txt";
            string image = ".png|.jpg|.bmp|.gif";
            string table = ".xls|.ods";

            if (text.IndexOf(extension) != -1) return fileIconText;
            if (image.IndexOf(extension) != -1) return fileIconImage;
            if (table.IndexOf(extension) != -1) return fileIconTable;
            else return fileIcon;
        }

        #endregion

        //public ListView listView;

        #region Navigate

        /*/

        public void GetDrives(ref TreeView parent, ref ListView lv, string mainNodeName)
        {
            parent.Nodes.Add(new TreeNode(mainNodeName));

            listView = lv;

            TreeNodeCollection drives = parent.Nodes[0].Nodes;

            string[] drivesArray = Environment.GetLogicalDrives();

            foreach (string s in drivesArray)
            {
                string sx = s.Split(new string[] { "\\" }, StringSplitOptions.None)[0];
                try
                {
                    if (Directory.GetDirectories(s).Length != 0)
                        drives.Add(sx).Nodes.Add("nix");
                }
                catch (IOException)
                {
                    drives.Add(sx);
                }
                drives[drives.Count - 1].Name = sx;
            }
        }

        public TreeNode[] GetSubfolders(TreeNode parent)
        {
            DirectoryInfo dir = new DirectoryInfo(parent.Name);

            DirectoryInfo[] subdirs = dir.GetDirectories();
            TreeNode[] retval = new TreeNode[subdirs.Length];

            for (int i = 0; i < subdirs.Length; i++)
            {
                retval[i] = new TreeNode(subdirs[i].Name);
                retval[i].Name = subdirs[i].FullName;
                try
                {
                    if (subdirs[i].GetDirectories().Length != 0)
                        retval[i].Nodes.Add("nix");
                }
                catch (UnauthorizedAccessException) { }
            }

            return retval;
        }

        //*/

        static Bitmap fileIconText, fileIconTable, fileIconImage, fileIcon, mapIcon;
        
        public IconButton[] GetFiles(string map, Size size, int returnWidth, int padding)
        {
            IconSize = size;
            Padding = padding;
            currentPath = map;
            ReturnWidth = returnWidth;

            fileIcon = fileIconText = fileIconTable = fileIconImage = mapIcon = null;
            fileIcon = new Bitmap(Options.LayoutFolder + "fileIcon.png");
            fileIconText = new Bitmap(Options.LayoutFolder + "fileIconText.png");
            fileIconTable = new Bitmap(Options.LayoutFolder + "fileIconTable.png");
            fileIconImage = new Bitmap(Options.LayoutFolder + "fileIconImage.png");
            mapIcon = new Bitmap(Options.LayoutFolder + "mapIcon.png");
            
            DirectoryInfo dir = null;
            try { dir = new DirectoryInfo(map); }
            catch (Exception)
            {
                Host.Feedback("(!) Error in creating directory info!", this);
                return null;
            }

            if (!dir.Exists)
            {
                Host.Feedback("(!) Directory not found!", this);
                return null;
            }

            DirectoryInfo[] subdirs = dir.GetDirectories();
            FileInfo[] files = dir.GetFiles();
            IconButton[] retval = new IconButton[subdirs.Length + files.Length];

            int noOfCols = returnWidth / (size.Width + padding);

            for (int i = 0; i < subdirs.Length; i++)
            {
                retval[i] = new IconButton();
                retval[i].Text = subdirs[i].Name;
                retval[i].Name = subdirs[i].FullName;
                retval[i].Size = size;
                retval[i].Icon = mapIcon;
                retval[i].MouseClick += new MouseEventHandler(FileBrowser_MouseClick);
                retval[i].MouseDoubleClick += new MouseEventHandler(FileBrowser_MouseDoubleClick);
                retval[i].Location = new Point((i % noOfCols) * (size.Width + padding) + padding, ((int)(i / noOfCols) * (size.Height + padding)));
            }

            for (int i = subdirs.Length; i < files.Length + subdirs.Length; i++)
            {
                retval[i] = new IconButton();
                retval[i].Text = files[i - subdirs.Length].Name;
                retval[i].Name = files[i - subdirs.Length].FullName;
                retval[i].Size = size;
                retval[i].Icon = GetFileIcon(files[i - subdirs.Length].Extension);
                retval[i].MouseClick += new MouseEventHandler(FileBrowser_MouseClick);
                retval[i].MouseDoubleClick += new MouseEventHandler(FileBrowser_MouseDoubleClick);
                retval[i].Location = new Point((i % noOfCols) * (size.Width + padding) + padding, ((int)(i / noOfCols) * (size.Height + padding)));
            }

            dir = null;
            subdirs = null;
            files = null;

            currentPath = map;

            return retval;
        }

        void FileBrowser_MouseClick(object sender, MouseEventArgs e)
        {
            
        }

        void FileBrowser_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Host.Act("navigate", new string[] { ((IconButton)sender).Name });
        }

        private void GetFiles(string p)
        {
            lvButtons = GetFiles(p, IconSize, ReturnWidth, Padding);
            if (lvButtons == null) return;
            this.Controls.Clear();
            this.Controls.AddRange(lvButtons);
            lvButtons = null;
            this.Refresh();
        }

        #endregion
    }
}
